package cn.le.netty;

import cn.le.bean.vo.NettyServerAddressVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

class ImUserUtils {

    private static ImUserUtils INSTANCE;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @PostConstruct
    private void setUp() {
        INSTANCE = this;
    }

    public static void login(String key,String value){
        INSTANCE.redisTemplate.opsForHash().put("login",key,value);
    }

    public static String getLogin(String key){
        return (String) INSTANCE.redisTemplate.opsForHash().get("login",key);
    }

    public static List<NettyServerAddressVO> getGroup(String key){
        Map<Object, Object> memberMap = INSTANCE.redisTemplate.opsForHash().entries("im_group:"+key);
        List<Object> memberIds = memberMap.entrySet().stream().map(o->o.getKey()).collect(Collectors.toList());
        return RedisUtils.NettyUtils.getConnects(memberIds);
    }

}
